#include "maininterface.h"
#include "ui_maininterface.h"
#include"dialog/friendreqdialog.h"
#include"dialog/groupchatdialog.h"
#include"dialog/aichatdialog.h"
#include"tcpmgr.h"
#include "usermgr.h"
#include"save.h"
#include "globalsignal.h"
#include<QThread>
#include <QMouseEvent>
#include <QTimer>

MainInterface::MainInterface(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MainInterface)
{
    ui->setupUi(this);
    _parentWindow = qobject_cast<MainWindow*>(this->parent());

    _display = ui->chat;
    _display->setFlat(false);
    ui->stackedWidget->setCurrentIndex(0);
    ui->lineEdit_2->installEventFilter(this);

    connect(TcpMgr::GetInstance().get(), &TcpMgr::sig_search_user, this, &MainInterface::slot_search_user);
    connect(TcpMgr::GetInstance().get(), &TcpMgr::sig_add_friend_req, this, [this]{
        ui->contacts->setIcon(QPixmap(":/img/qrc/img/contactsRedDot.png"));
    });
    connect(GlobalSignal::GetInstance().get(), &GlobalSignal::sig_add_friend_req, this, [this]{
        ui->contacts->setIcon(QPixmap(":/img/qrc/img/contactsRedDot.png"));
    });
    connect(ui->chatDialog, &ChatDialog::sig_switch_user, this, [this](const QString& name){
        ui->label->setText(name);
    });
    connect(ui->contactsDialog, &ContactsDialog::sig_switch_user, ui->chatDialog, [this](const QString& name){
        ui->chat->click();
        ui->chatDialog->slot_contacts_switch_user(name);
    });
    heartbeat = new QTimer(this);
    connect(heartbeat, &QTimer::timeout, this, []{
        emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_HEARTBEAT, QByteArray());
    });

    heartbeat->start(60000);
}

MainInterface::~MainInterface()
{
    heartbeat->stop();
    Save::GetInstance()->Close();
    delete ui;
    delete heartbeat;
    qDebug("MainInterface::~MainInterface");
}

void MainInterface::setPixmap(const QPixmap &pixmap)
{
    ui->avatar->setPixmap(pixmap);
}

bool MainInterface::eventFilter(QObject *watched, QEvent *event)
{
    if (ui->lineEdit_2 == watched)
    {
        if (event->type() == QEvent::FocusIn)
        {
            qDebug("获得焦点");
        } else if (event->type() == QEvent::FocusOut)
        {
            qDebug("失去焦点");
        }
    }
    return QObject::eventFilter(watched, event);
}

void MainInterface::ChangeStyle(QPushButton* qpb, int indexes)
{
    if (_display == qpb) {
        return;
    }
    _display->setFlat(true);
    _display = qpb;
    _display->setFlat(false);

    ui->stackedWidget->setCurrentIndex(indexes);
}

void MainInterface::on_chat_clicked()
{
    ui->chat->setIcon(QPixmap(":/img/qrc/img/chat.png"));
    ChangeStyle(ui->chat,0);
    _currentPage = 0;
}

void MainInterface::on_contacts_clicked()
{
    ui->contacts->setIcon(QPixmap(":/img/qrc/img/contacts.png"));
    ChangeStyle(ui->contacts,1);
    _currentPage = 1;
}

void MainInterface::on_collect_clicked()
{
    ChangeStyle(ui->collect,2);
    _currentPage = 2;
}

void MainInterface::on_search_clicked()
{
    QString user{ui->lineEdit_2->text()};
    if (user.isEmpty()){
        return;
    }
    if (user == UserMgr::GetInstance()->getName()){
        QMessageBox::information(NULL, "提示", "你不能添加自己为好友", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }

    isGetAvatar(user);

    QJsonObject json_obj;
    json_obj["token"] = UserMgr::GetInstance()->getToken();
    json_obj["name"] = user;
    QJsonDocument doc(json_obj);
    emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_SEARCH_USER_REQ, doc.toJson(QJsonDocument::Compact));
}

void MainInterface::slot_search_user(int uid)
{
    //需要添加用户名和uid
    if (uid == 0){
        QMessageBox::information(NULL, "提示", "没有找到用户", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        return;
    }
    friendReqDialog *f = new friendReqDialog(this);
    f->setUser(ui->lineEdit_2->text(), "我是" + UserMgr::GetInstance()->getName());
    f->setAttribute(Qt::WA_DeleteOnClose);
    f->setWindowModality(Qt::ApplicationModal);
    f->show();
}

void MainInterface::on_logOutBtn_clicked()
{
    emit sig_exit_chat();
    QJsonObject jsonObj;
    jsonObj["token"] = UserMgr::GetInstance()->getToken();
    QJsonDocument doc(jsonObj);
    emit TcpMgr::GetInstance()->sig_send_data(ReqId::ID_QUIT_LOGIN, doc.toJson(QJsonDocument::Compact));
    TcpMgr::GetInstance()->Close();
}

void MainInterface::on_aiBtn_clicked()
{
    AIChatDialog *a = new AIChatDialog;
    a->setAttribute(Qt::WA_DeleteOnClose);
    a->setWindowTitle("ai");
    a->show();
}


void MainInterface::on_groupBtn_clicked()
{
    GroupChatDialog *g = new GroupChatDialog(this);
    g->setAttribute(Qt::WA_DeleteOnClose);
    g->show();
    connect(g, &GroupChatDialog::sig_create_group, ui->chatDialog, &ChatDialog::slot_add_group);
}
